package com.lntea.jdbc.dbcp;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.dbcp.ConnectionFactory;
import org.apache.commons.dbcp.DriverManagerConnectionFactory;
import org.apache.commons.dbcp.PoolableConnectionFactory;
import org.apache.commons.dbcp.PoolingDriver;
import org.apache.commons.dbutils.BasicRowProcessor;
import org.apache.commons.dbutils.BeanProcessor;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.ResultSetHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.pool.ObjectPool;
import org.apache.commons.pool.impl.GenericObjectPool;

import com.lntea.jdbc.dbutils.User;

public class PoolingDriverDemo {
	public static void main(String[] args) {
		// 创建connection工厂
		ConnectionFactory factory = new DriverManagerConnectionFactory("jdbc:mysql://localhost:3306/jfinalweb", "root",
				"root");
		// 连接池
		ObjectPool pool = new GenericObjectPool();
		// 池化connection工厂
		// PoolableConnectionFactory(ConnectionFactory connFactory, ObjectPool
		// pool,
		// KeyedObjectPoolFactory stmtPoolFactory, String validationQuery,
		// boolean defaultReadOnly,
		// boolean defaultAutoCommit)
		PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(factory, pool, null, null,
				false, true);
		
		try {
			Class.forName("org.apache.commons.dbcp.PoolingDriver");
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		
		try {
			PoolingDriver poolingDriver = (PoolingDriver) DriverManager.getDriver("jdbc:apache:commons:dbcp:");
			poolingDriver.registerPool("demo", pool);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		Connection conn = null;
		try {
			conn = DriverManager.getConnection("jdbc:apache:commons:dbcp:demo");
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		execQuery(conn);
		printDriverStats();
	}
	
	static void printDriverStats() {
		try {
			PoolingDriver driver = (PoolingDriver) DriverManager.getDriver("jdbc:apache:commons:dbcp:");
			ObjectPool pool = driver.getConnectionPool("demo");
			
			System.out.println("Num Active:"+pool.getNumActive());
			System.out.println("Num Idle:"+pool.getNumIdle());
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}

	static void execQuery(Connection conn) {
		Map<String, String> map = new HashMap<String, String>();
		map.put("password_salt", "passwordSalt");
		BeanProcessor customBeanProcessor = new BeanProcessor(map);
		ResultSetHandler<List<User>> h = new BeanListHandler<User>(User.class,new BasicRowProcessor(customBeanProcessor));

		QueryRunner queryRunner = new QueryRunner();
		List<User> users = null;
		try {
			users = queryRunner.query(conn, "select * from users", h);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		for (User user : users)
			System.out.println(user);
	}
}
